package com.lninl.util;

import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Spider {
    private String url;
    private String title;
    private String description;
    private String body;
    private String content;

    public Spider(String url) {
        this.url = url;
        try {
            this.content = getContentByUrl(this.url);

            List<String> list = new ArrayList<>();
            String title = "";

            Pattern pa = Pattern.compile("<title>.*?</title>");// 源码中标题正则表达式
            Matcher ma = pa.matcher(this.content);
            while (ma.find())// 寻找符合el的字串
            {
                list.add(ma.group());// 将符合el的字串加入到list中
            }
            for (int i = 0; i < list.size(); i++) {
                title = title + list.get(i);
            }

            this.title = removeOutTag(title);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    private String getContentByUrl(String url) throws Exception {
        InputStreamReader in = new InputStreamReader(new URL(url).openStream());
        // read contents into string buffer
        StringBuilder input = new StringBuilder();
        int ch;
        while ((ch = in.read()) != -1)
            input.append((char) ch);
        in.close();
        return input.toString();
    }

    /**
     * 去掉html源码中的标签
     *
     * @param s
     * @return
     */
    private String removeOutTag(String s) {
        return s.replaceAll("<.*?>", "");
    }

}
